/*===========================
nMSXtiles
http://pentacour.com
============================*/

#include "mainwindow.h"
#include "screenw.h"
#include "spritesw.h"
#include <QMenuBar>
#include <QAction>
#include <QDockWidget>
#include <QFileDialog>
#include <QMessageBox>
#include <QCloseEvent>
#include <QSettings>
#include <QDebug>
#include "fdlgloadbiblio.h"
#include "dlgsubscreen.h"
#include "dlgexportscreen.h"
#include "dlgexchangetiles.h"

CMainWindow::CMainWindow(): QMainWindow( )
{
    m_Version = "0.8.6";

    QSettings settings( "Pentacour", "nMSXTiles" );
    m_LastPathLoadProject = settings.value( "LoadProject" ).toString();
    m_LastPathExport = settings.value( "Export" ).toString();
    m_LastFormat = settings.value( "Format" ).toInt();
    m_LastFilesMode = settings.value( "FilesMode" ).toInt();
    m_LastWidth = settings.value( "Width" ).toInt();
    m_LastHeight = settings.value( "Height" ).toInt();
    m_LastX = settings.value( "X").toInt();
    m_LastY = settings.value( "Y" ).toInt();



    if( m_LastPathLoadProject == "" ) {
        m_LastPathLoadProject = QDir::currentPath();
    }

    m_pActNewProject = new QAction( tr( "New" ), this );
    m_pActLoadProject = new QAction( tr( "Load" ), this );
    m_pActSaveProject = new QAction( tr( "Save" ), this );
    m_pActSaveAsProject = new QAction( tr( "Save as..." ), this );
    m_pActCloseProject = new QAction( tr( "Close" ), this );
    m_pActImportPolkaProject = new QAction( tr( "Import Polka! Project" ), this );
    m_pActExportPolkaProject = new QAction( tr( "Export Polka! Project" ), this );
    m_pActQuit = new QAction( tr( "Quit" ), this );

    m_pActNewScreen = new QAction( tr( "New" ), this );
    m_pActLoadScreen = new QAction( tr( "Load" ), this );
    m_pActSaveScreen = new QAction( tr( "Save" ), this );
    m_pActSaveAsScreen = new QAction( tr( "Save as..." ), this );
    m_pActExportScreen = new QAction( tr( "Export Screen..." ), this );
    m_pActExportScreenPNG = new QAction( tr( "Export Screen PNG file"), this );
    m_pActExportMapPNG = new QAction( tr( "Export Map PNG file"), this );
    m_pActFillScreen1to255 = new QAction( tr( "Fill Screen with tiles 1 to 255"), this );
    m_pActExchangeTiles = new QAction( tr( "Exchange tiles" ), this );

    m_pActNewTiles = new QAction( tr( "New" ), this );
    m_pActLoadTiles = new QAction( tr( "Load" ), this );
    m_pActSaveTiles = new QAction( tr( "Save" ), this );
    m_pActSaveAsTiles = new QAction( tr( "Save as..." ), this );
    m_pActLoadTilesLibrary = new QAction( tr( "Load from library..." ), this );
    m_pActExportTilesData = new QAction( tr( "Export ASM data..." ), this );
    m_pActExportTilesDataHexa = new QAction( tr( "Export ASM data Hexadecimal..." ), this );
    m_pActExportTilesBin = new QAction( tr( "Export bin data..." ), this );
    m_pActExportTilesBinPletter = new QAction( tr( "Export bin data compressed with Pletter..." ), this );
    m_pActExportTilesPNG = new QAction( tr( "Export PNG file"), this );
    m_pActImportTilesPNG = new QAction( tr( "Import PNG file created with nMSXtiles"), this );
    m_pActGenPaletteFile = new QAction( tr( "Create PNG Palette file" ), this );
//    m_pActImportTilesPNGAdv = new QAction( tr( "Import PNG without palette"), this );
        m_pActImportTiles = new QAction( tr( "Import..." ), this );
        m_pActGroupTiles = new QAction( tr( "Group Tiles" ), this );

        m_pActReorderColors = new QAction( tr("Reorder colors" ), this );
        m_pActCopyTiles = new QAction( tr( "Copy" ), this );
        m_pActCopyTiles->setShortcut(tr("Ctrl+C"));
        m_pActPasteTiles = new QAction( tr( "Paste" ), this );
        m_pActPasteTiles->setShortcut(tr("Ctrl+V"));
        m_pActPasteTilesColors = new QAction( tr( "Paste colors" ), this );
        m_pActPasteTilesColors->setShortcut(tr("Ctrl+G"));
        m_pActUndo = new QAction( tr( "Undo" ), this );
        m_pActUndo->setShortcut( tr( "Ctrl+Z" ) );

        m_pActNewSprites = new QAction( tr( "New" ), this );
        m_pActLoadSprites = new QAction( tr( "Load" ), this );
        m_pActSaveSprites = new QAction( tr( "Save" ), this );
        m_pActSaveAsSprites = new QAction( tr( "Save as..." ), this );
        m_pActCloseSprites = new QAction( tr( "Close" ), this );
        m_pActExportSprites = new QAction( tr( "Export..." ), this );

        m_pActAbout = new QAction( tr( "nMSXtiles" ), this );

        connect( m_pActNewProject, SIGNAL( triggered() ), this, SLOT( OnNewProject() ) );
        connect( m_pActLoadProject, SIGNAL( triggered() ), this, SLOT( OnLoadProject() ) );
        connect( m_pActSaveProject, SIGNAL( triggered() ), this, SLOT( OnSaveProject() ) );
        connect( m_pActSaveAsProject, SIGNAL( triggered() ), this, SLOT( OnSaveAsProject() ) );
        connect( m_pActCloseProject, SIGNAL( triggered() ), this, SLOT( OnCloseProject() ) );
        connect( m_pActImportPolkaProject, SIGNAL( triggered()), this, SLOT( OnImportPolkaProject() ) );
        connect( m_pActExportPolkaProject, SIGNAL( triggered() ), this, SLOT( OnExportPolkaProject() ) );
        connect( m_pActQuit, SIGNAL( triggered() ), this, SLOT( OnQuit() ) );
        connect( m_pActNewScreen, SIGNAL( triggered() ), this, SLOT( OnNewScreen() ) );
        connect( m_pActLoadScreen, SIGNAL( triggered() ), this, SLOT( OnLoadScreen() ) );
        connect( m_pActSaveScreen, SIGNAL( triggered() ), this, SLOT( OnSaveScreen() ) );
        connect( m_pActSaveAsScreen, SIGNAL( triggered() ), this, SLOT( OnSaveAsScreen() ) );
        connect( m_pActExportScreen, SIGNAL( triggered()), this, SLOT( OnExportScreen() ) );
        connect( m_pActExportScreenPNG, SIGNAL( triggered() ), this, SLOT( OnExportScreenPNG() ) );
        connect( m_pActExportMapPNG, SIGNAL( triggered() ), this, SLOT( OnExportMapPNG() ) );
        connect( m_pActNewTiles, SIGNAL( triggered() ), this, SLOT( OnNewTiles() ) );
        connect( m_pActLoadTiles, SIGNAL( triggered() ), this, SLOT( OnLoadTiles() ) );
        connect( m_pActSaveTiles, SIGNAL( triggered() ), this, SLOT( OnSaveTiles() ) );
        connect( m_pActSaveAsTiles, SIGNAL( triggered() ), this, SLOT( OnSaveAsTiles() ) );
        connect( m_pActLoadTilesLibrary, SIGNAL( triggered() ), this, SLOT( OnLoadTilesLibrary() ) );
        connect( m_pActExportTilesData, SIGNAL( triggered() ), this, SLOT( OnExportTilesData() ) );
        connect( m_pActExportTilesDataHexa, SIGNAL( triggered() ), this, SLOT( OnExportTilesDataHexa() ) );
        connect( m_pActExportTilesBin, SIGNAL( triggered() ), this, SLOT( OnExportTilesBin() ) );
        connect( m_pActExportTilesBinPletter, SIGNAL( triggered() ), this, SLOT( OnExportTilesBinPletter() ) );
    connect( m_pActFillScreen1to255, SIGNAL( triggered() ), this, SLOT( OnFillScreen1to255() ));
    connect( m_pActExchangeTiles, SIGNAL( triggered() ), this, SLOT( OnExchangeTiles() ) );
    connect( m_pActExportTilesPNG, SIGNAL( triggered() ), this, SLOT( OnExportTilesPNG()) );
    connect( m_pActImportTilesPNG, SIGNAL( triggered()), this, SLOT( OnImportTilesPNG()));
    connect( m_pActGenPaletteFile, SIGNAL( triggered() ), this, SLOT( OnGenPaletteFile()) );
//    connect( m_pActImportTilesPNGAdv, SIGNAL( triggered() ), this, SLOT( OnImportTilesPNGAdv() ) );
        connect( m_pActImportTiles, SIGNAL( triggered() ), this, SLOT( OnImportTiles() ) );
        connect( m_pActGroupTiles, SIGNAL( triggered() ), this, SLOT( OnGroupTiles() ) );
    connect( m_pActReorderColors, SIGNAL( triggered() ), this, SLOT( OnReorderColors() ) );
    connect( m_pActCopyTiles, SIGNAL( triggered() ), this, SLOT( OnCopyTiles() ) );
    connect( m_pActPasteTiles, SIGNAL( triggered() ), this, SLOT( OnPasteTiles() ) );
    connect( m_pActPasteTilesColors, SIGNAL( triggered() ), this, SLOT( OnPasteTilesColors() ) );
    connect( m_pActUndo, SIGNAL( triggered() ), this, SLOT( OnUndo() ) );

        connect( m_pActNewSprites, SIGNAL( triggered() ), this, SLOT( OnNewSprites() ) );
        connect( m_pActLoadSprites, SIGNAL( triggered() ), this, SLOT( OnLoadSprites() ) );
        connect( m_pActSaveSprites, SIGNAL( triggered() ), this, SLOT( OnSaveSprites() ) );
        connect( m_pActSaveAsSprites, SIGNAL( triggered() ), this, SLOT( OnSaveAsSprites() ) );
        connect( m_pActCloseSprites, SIGNAL( triggered() ), this, SLOT( OnCloseSprites() ) );
        connect( m_pActExportSprites, SIGNAL( triggered() ), this, SLOT( OnExportSprites() ) );

        connect( m_pActAbout, SIGNAL( triggered() ), this, SLOT( OnAbout() ) );

        m_pMenu = menuBar()->addMenu( tr( "&Project" ) );
        m_pMenu->addAction( m_pActNewProject );
        m_pMenu->addAction( m_pActLoadProject );
        m_pMenu->addAction( m_pActSaveProject );
        m_pMenu->addAction( m_pActSaveAsProject );
        m_pMenu->addAction( m_pActCloseProject );
        m_pMenu->addAction( m_pActImportPolkaProject );
        m_pMenu->addAction( m_pActExportPolkaProject );
        m_pMenu->addAction( m_pActQuit );

//      m_pMenu = menuBar()->AddMenu( tr( "Edit" ) );
        //m_pMenu->addAction( m_pActCopy );
        //m_pMenu->addAction( m_pActPaste );

        m_pMenu = menuBar()->addMenu( tr( "Screen" ) );
        m_pMenu->addAction( m_pActNewScreen );
        m_pMenu->addAction( m_pActLoadScreen );
        m_pMenu->addAction( m_pActSaveScreen );
        m_pMenu->addAction( m_pActSaveAsScreen );
        m_pMenu->addAction( m_pActExportScreen );
/*        m_pMenu->addAction( m_pActExportScreenData );
        m_pMenu->addAction( m_pActExportScreenDataHexa );
        m_pMenu->addAction( m_pActExportScreenBin );
        m_pMenu->addAction( m_pActExportScreenBinPletter );*/
    m_pMenu->addAction( m_pActExportMapPNG );
    m_pMenu->addAction( m_pActExportScreenPNG );
    m_pMenu->addSeparator();
    m_pMenu->addAction( m_pActFillScreen1to255 );
    m_pMenu->addAction( m_pActExchangeTiles );



        m_pMenu = menuBar()->addMenu( tr( "Tiles" ) );
        m_pMenu->addAction( m_pActNewTiles );
        m_pMenu->addAction( m_pActLoadTiles );
        m_pMenu->addAction( m_pActSaveTiles );
        m_pMenu->addAction( m_pActSaveAsTiles );
        m_pMenu->addAction( m_pActLoadTilesLibrary );
        m_pMenu->addAction( m_pActExportTilesData );
        m_pMenu->addAction( m_pActExportTilesDataHexa );
        m_pMenu->addAction( m_pActExportTilesDataHexa );
        m_pMenu->addAction( m_pActExportTilesBin );
        m_pMenu->addAction( m_pActExportTilesBinPletter );
    m_pMenu->addAction( m_pActExportTilesPNG );
    m_pMenu->addAction( m_pActImportTilesPNG );
//    m_pMenu->addAction( m_pActImportTilesPNGAdv );
    m_pMenu->addAction( m_pActImportTiles );
    m_pMenu->addAction( m_pActGenPaletteFile );


    m_pMenu = menuBar()->addMenu( tr( "Tiles Tools" ) );
    m_pMenu->addAction( m_pActReorderColors );
    m_pMenu->addAction( m_pActCopyTiles );
    m_pMenu->addAction( m_pActPasteTiles );
    m_pMenu->addAction( m_pActPasteTilesColors );
    m_pMenu->addAction( m_pActUndo );
    m_pMenu->addAction( m_pActGroupTiles );

    m_pMenu = menuBar()->addMenu( tr( "Sprites" ) );
    m_pMenu->addAction( m_pActNewSprites );
    m_pMenu->addAction( m_pActLoadSprites );
    m_pMenu->addAction( m_pActSaveSprites );
    m_pMenu->addAction( m_pActSaveAsSprites );
    m_pMenu->addAction( m_pActCloseSprites );
    m_pMenu->addAction( m_pActExportSprites );

        m_pMenu = menuBar()->addMenu( tr( "About" ) );
        m_pMenu->addAction( m_pActAbout );

        m_pWks = new QMdiArea;
        setCentralWidget( m_pWks );

        m_pScreen = NULL;
        m_pSprites = NULL;

        resize( 450, 600 );

        m_ProjectFile = "";
        m_ScreenFile = "";
        m_TilesFile = "";



        setWindowTitle( QString( "nMSXtiles - v%1" ).arg( m_Version ) );
        setWindowIcon( QIcon(QString::fromUtf8(":/new/res/icono.png") ) );

        EnableMenuItems();
}


void CMainWindow::OnNewProject()
{
        if( m_pScreen && QMessageBox::question( this, "Confirm", "Save Project?", QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )    OnSaveAsProject();

        if( m_pScreen ) {
            m_pScreen->close();
            delete m_pScreen;
        }

        m_pScreen = new CScreenW();
        m_pScreen->Initialize();
        m_pWks->addSubWindow( m_pScreen, Qt::Dialog );
        m_pScreen->show();
        m_pScreen->NewTiles();
        m_pScreen->NewScreen();

        m_ProjectFile = "";
        m_ScreenFile = "";
        m_TilesFile = "";

        EnableMenuItems();
}

void CMainWindow::OnLoadProject()
{
        int i;
        QString relPath;
        QFile hFile;
        QString fileName;


        if( m_pScreen ) OnCloseProject();

        fileName = QFileDialog::getOpenFileName( this, "Project - Load", m_LastPathLoadProject, "nMSXtiles Project (*.prj);;All files (*.*)" );

        if( fileName == "" ) return;

        m_LastPathLoadProject = fileName.left( fileName.lastIndexOf( "/" ) );

        m_ProjectFile = fileName;

        hFile.setFileName( m_ProjectFile );
        hFile.open( QIODevice::ReadOnly );

        QTextStream str( &hFile );

        if( hFile.atEnd() )
        {
                hFile.close();
                QMessageBox::critical( this, "Error", "Unrecognized format file" );
                return;
        }

        if( str.readLine() != "PROJECT" )
        {
                hFile.close();
                QMessageBox::critical( this, "Error", "Unrecognized format file" );
                return;
        }

        if( str.readLine() != "SCREEN" )
        {
                hFile.close();
                QMessageBox::critical( this, "Error", "Unrecognized format file" );
                return;
        }

        i = m_ProjectFile.lastIndexOf( "/" );

        relPath = m_ProjectFile.left( i );

        m_ScreenFile = str.readLine();

        if( m_ScreenFile.lastIndexOf( "/" ) == -1 ) m_ScreenFile = relPath + QString( "/" ) + m_ScreenFile;

        m_pScreen = new CScreenW();
        m_pScreen->Initialize();
        m_pWks->addSubWindow( m_pScreen, Qt::Dialog );
        m_pScreen->show();

        m_pScreen->LoadScreen( m_ScreenFile );

        if( str.readLine() != "TILES" )
        {
                hFile.close();
                QMessageBox::critical( this, "Error", "Unrecognized format file" );
                return;
        }

        m_TilesFile = str.readLine();

        if( m_TilesFile.lastIndexOf( "/" ) == -1 ) m_TilesFile = relPath + QString( "/" ) + m_TilesFile;

        m_pScreen->LoadTiles( m_TilesFile );

        if( str.readLine() == "ONEBANK" ) m_pScreen->SetOneBank( true );
        else m_pScreen->SetOneBank( false );

        hFile.close();

        EnableMenuItems();
}

void CMainWindow::OnImportPolkaProject()
{
        QString relPath;
        QFile hFile;
        QString fileName;


        if( m_pScreen ) OnCloseProject();

        fileName = QFileDialog::getOpenFileName( this, "Import Polka! project", m_LastPathLoadProject, "Polka! project (*.ppr);;All files (*.*)" );

        if( fileName == "" ) return;

        m_LastPathLoadProject = fileName.left( fileName.lastIndexOf( "/" ) );
                m_pScreen = new CScreenW();
                m_pScreen->Initialize();
                m_pWks->addSubWindow( m_pScreen, Qt::Dialog );
                m_pScreen->show();
//        m_pScreen->ImportPolkaScreen( fileName );
        m_pScreen->ImportPolkaTiles( fileName );

        EnableMenuItems();
}

void CMainWindow::OnExportPolkaProject()
{
        QString fileName;


        fileName = QFileDialog::getSaveFileName( this, "Export Polka! project", m_LastPathExport, "Polka! project (*.ppr);;All files (*.*)" );

        if( fileName > "" )
        {
                m_LastPathExport = fileName.left( fileName.lastIndexOf( "/" ) );
                m_pScreen->ExportPolkaTiles( fileName );
        }
}

void CMainWindow::OnSaveProject()
{
        if( m_ProjectFile == "" ) OnSaveAsProject();
        else SaveProject();
}

void CMainWindow::OnSaveAsProject()
{
        QString fileName;


        if( m_ScreenFile == "" ) OnSaveAsScreen();
        else m_pScreen->SaveScreen( m_ScreenFile );

        if( m_TilesFile == "" ) OnSaveAsTiles();
        else m_pScreen->SaveTiles( m_TilesFile );

        if( m_ScreenFile == "" || m_TilesFile == "" )
        {
                QMessageBox::question( this, "Project not saved!", "Warning" );
                return;
        }

        m_ProjectFile = QFileDialog::getSaveFileName( this, "Project - Save as", m_LastPathLoadProject, "nMSXtiles Project (*.prj);;All files (*.*)" );

        qDebug()<<"CMainWindow::OnSaveASProject ProjectFile="<<m_ProjectFile;

        m_LastPathLoadProject = m_ProjectFile.left( m_ProjectFile.lastIndexOf( "/" ) );

        if( m_ProjectFile > "" ) SaveProject();
        else QMessageBox::question( this, "Project not saved!", "Warning" );
}

void CMainWindow::SaveProject()
{
        int i;
        QFile hFile;
        QString projectFile;
        QString tilesFile;
        QString screenFile;

        if( m_ProjectFile == "" ) OnSaveAsProject();
        else
        {
                OnSaveScreen();
                OnSaveTiles();

                i = m_ScreenFile.lastIndexOf( "/" );
                if( i == -1 ) screenFile = m_ScreenFile;
                else screenFile = m_ScreenFile.left( i );

                i = m_TilesFile.lastIndexOf( "/" );
                if( i == -1 ) tilesFile = m_TilesFile;
                else tilesFile = m_TilesFile.left( i );

                i = m_ProjectFile.lastIndexOf( "/" );
                if( i == -1 ) projectFile = m_ProjectFile;
                else projectFile = m_ProjectFile.left( i );

                if( screenFile == projectFile && tilesFile == projectFile )
                {
                        if( i == -1 )
                        {
                                projectFile = m_ProjectFile;
                                screenFile = m_ScreenFile;
                                tilesFile = m_TilesFile;
                        }
                        else
                        {
                                projectFile = m_ProjectFile.right( m_ProjectFile.size() - m_ProjectFile.lastIndexOf( "/" ) - 1 );
                                screenFile = m_ScreenFile.right( m_ScreenFile.size() - m_ScreenFile.lastIndexOf( "/" ) - 1 );
                                tilesFile = m_TilesFile.right( m_TilesFile.size() - m_TilesFile.lastIndexOf( "/" ) - 1 );
                        }
                }
                else
                {
                        projectFile = m_ProjectFile;
                        screenFile = m_ScreenFile;
                        tilesFile = m_TilesFile;
                }

                hFile.setFileName( m_ProjectFile );
                hFile.open( QIODevice::WriteOnly );

                QTextStream str( &hFile );

                str<<"PROJECT"<<endl;
                str<<"SCREEN"<<endl;
                str<<screenFile<<endl;
                str<<"TILES"<<endl;
                str<<tilesFile<<endl;

                if( m_pScreen->GetOneBank() ) str<<"ONEBANK"<<endl;
                else str<<"ALLBANKS"<<endl;

                hFile.flush();
                hFile.close();
        }
}

void CMainWindow::OnCloseProject()
{
        if( m_pScreen && QMessageBox::question( this, "Confirm", "Save Project?", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )     OnSaveProject();

        m_pScreen->Close();
        m_pWks->closeActiveSubWindow();
        delete m_pScreen;
        m_pScreen = NULL;

        EnableMenuItems();
}

void CMainWindow::OnNewScreen()
{
        OnSaveScreen();
        m_pScreen->NewScreen();
}

void CMainWindow::OnLoadScreen()
{
        QFile hFile;
        QString line;
        QString fileName;


        fileName = QFileDialog::getOpenFileName( this, "Screen - Load", m_LastPathLoadProject, "nMSXtiles Screen (*.scr);;All files (*.*)" );

        if( fileName == "" ) return;

        m_LastPathLoadProject = fileName.left( fileName.lastIndexOf( "/" ) );

        m_ScreenFile = fileName;

        m_pScreen->LoadScreen( m_ScreenFile );
}


void CMainWindow::OnSaveScreen()
{
        if( m_ScreenFile == "" ) OnSaveAsScreen();
        else m_pScreen->SaveScreen( m_ScreenFile );
}

void CMainWindow::OnSaveAsScreen()
{
        QString fileName;


        fileName = QFileDialog::getSaveFileName( this, "Screen - Save as", m_LastPathLoadProject, "nMSXtiles Screen (*.scr);;All files (*.*)" );

        if( fileName > "" )
        {
                m_ScreenFile = fileName;
                m_pScreen->SaveScreen( m_ScreenFile );
                m_LastPathLoadProject = fileName.left( fileName.lastIndexOf( "/" ) );
        }
}

void CMainWindow::OnExportScreen() {
    QString fileName;
    CDlgExportScreen *pExport;


    //pExport = new CDlgExportScreen(m_LastPathExport, this);
    //int lastFormat, int lastFilesMode, int lastWidth, int lastHeight, int lastX, int lastY, QWidget *pParent )
    pExport = new CDlgExportScreen(m_LastPathExport, m_LastFormat, m_LastFilesMode, m_LastWidth, m_LastHeight, m_LastX, m_LastY);
    if( pExport->exec() == QDialog::Accepted ) {

        m_LastPathExport = pExport->get_FileName().left( fileName.lastIndexOf( "/" ) );
        m_LastFormat = pExport->get_Format();
        m_LastFilesMode = pExport->get_FileMode();
        m_LastWidth = pExport->get_Width();
        m_LastHeight = pExport->get_Height();
        m_LastX = pExport->get_X();
        m_LastY = pExport->get_Y();

        switch( pExport->get_Format() ) {
        case CDlgExportScreen::TYPE_ASM:
            m_pScreen->ExportScreenData( pExport->get_FileName(), false, pExport->get_X(), pExport->get_Y(), pExport->get_Width(), pExport->get_Height() );
            break;
        case CDlgExportScreen::TYPE_ASM_HEXA:
            m_pScreen->ExportScreenData( pExport->get_FileName(), true, pExport->get_X(), pExport->get_Y(), pExport->get_Width(), pExport->get_Height() );
            break;
        case CDlgExportScreen::TYPE_BIN:
            if( pExport->get_FileMode() == CDlgExportScreen::TYPE_ONE_WHOLE_MAP ) m_pScreen->ExportScreenBinScroll( pExport->get_FileName(), pExport->get_X(), pExport->get_Y(), pExport->get_Width(), pExport->get_Height() );
            else m_pScreen->ExportScreenBin( pExport->get_FileName(), pExport->get_X(), pExport->get_Y(), pExport->get_Width(), pExport->get_Height() );
            break;
        case CDlgExportScreen::TYPE_BIN_PLETTER:
            if( pExport->get_FileMode() == CDlgExportScreen::TYPE_ONE_WHOLE_MAP ){
               m_pScreen->ExportScreenBinPletterScroll( pExport->get_FileName(), pExport->get_X(), pExport->get_Y(), pExport->get_Width(), pExport->get_Height() );
            }
            else {
               m_pScreen->ExportScreenBinPletter( pExport->get_FileName(), pExport->get_X(), pExport->get_Y(), pExport->get_Width(), pExport->get_Height() );
            }
            break;

        }



    }
}

void CMainWindow::OnExportScreenPNG() {
    QString fileName;


    fileName = QFileDialog::getSaveFileName( this, "Screen - Export PNG file as", m_LastPathExport );

    if( fileName > "" )
    {
        m_LastPathExport = fileName.left( fileName.lastIndexOf( "/" ) );
        m_pScreen->ExportScreenPNG( fileName );
    }
}

void CMainWindow::OnExportMapPNG() {
    QString fileName;


    fileName = QFileDialog::getSaveFileName( this, "Map - Export PNG file as", m_LastPathExport );

    if( fileName > "" )
    {
        m_LastPathExport = fileName.left( fileName.lastIndexOf( "/" ) );
        m_pScreen->ExportMapPNG( fileName );
    }
}

void CMainWindow::OnFillScreen1to255()
{
    if( QMessageBox::question( this, "Confirm", "Fill Screen with tiles from 1 to 255", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )
    {
        m_pScreen->Fill1to255();
    }
}

void CMainWindow::OnExchangeTiles()
{
    CDlgExchangeTiles dlg( NULL );

    dlg.exec();

    if( dlg.result() == 0 )
        return;

    m_pScreen->ExchangeTiles( dlg.get_Tile1(), dlg.get_Tile2() );
}

void CMainWindow::OnQuitActions() {
    if( m_pScreen && QMessageBox::question( this, "Confirm", "Save Project?", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes ) {
        OnSaveAsProject();
    }

    if( m_pScreen ) m_pScreen->Close();
    m_pScreen = NULL;

    QSettings settings( "Pentacour", "nMSXTiles" );
    settings.setValue( "LoadProject", m_LastPathLoadProject ) ;
    settings.setValue( "Export", m_LastPathExport );
    settings.setValue( "Format", m_LastFormat );
    settings.setValue( "FilesMode", m_LastFilesMode );
    settings.setValue( "Width", m_LastWidth );
    settings.setValue( "Height", m_LastHeight );
    settings.setValue( "X", m_LastX);
    settings.setValue( "Y", m_LastY );

}

void CMainWindow::OnQuit()
{

        close();
}

void CMainWindow::OnAbout()
{
        QMessageBox::about( this, "nMSXtiles", QString( "Screens editor for MSX screen 2\n\nVersion %1\n\nRamon de las Heras Gomiz\nwww.pentacour.com" ).arg( m_Version ) );
}

void CMainWindow::OnNewTiles()
{
        OnSaveTiles();
        m_pScreen->NewTiles();
}

void CMainWindow::OnLoadTilesLibrary()
{
    FDlgLoadBiblio *pDlg;

    pDlg = new FDlgLoadBiblio( m_LastPathLoadProject );
    if( !pDlg->exec() ) return;

    if( pDlg->get_FileName() == "" ) return;

    m_LastPathLoadProject = pDlg->get_FileName().left( pDlg->get_FileName().lastIndexOf( "/" ) );

    m_pScreen->LoadTilesLibrary( pDlg->get_FileName(), pDlg->get_BankOr(), pDlg->get_XOr(), pDlg->get_YOr(), pDlg->get_Widht(), pDlg->get_Height(), pDlg->get_BankDest(), pDlg->get_XDest(), pDlg->get_YDest() );
}


void CMainWindow::OnLoadTiles()
{
        QFile hFile;
        QString line;
        QString fileName;


        fileName = QFileDialog::getOpenFileName( this, "Tiles - Load", m_LastPathLoadProject, "nMSXtiles Tiles (*.til);;All files (*.*)" );

        if( fileName == "" ) return;

        m_LastPathLoadProject = fileName.left( fileName.lastIndexOf( "/" ) );

        m_TilesFile = fileName;

        m_pScreen->LoadTiles( m_TilesFile );
}

void CMainWindow::OnSaveTiles()
{
        if( m_TilesFile == "" ) OnSaveAsTiles();
        else m_pScreen->SaveTiles( m_TilesFile );
}

void CMainWindow::OnSaveAsTiles()
{
        QString fileName;


        fileName = QFileDialog::getSaveFileName( this, "Tiles - Save as",  m_LastPathLoadProject, "nMSXtiles Tiles (*.til);;All files (*.*)" );

        if( fileName > "" )
        {
                m_TilesFile = fileName;
                m_pScreen->SaveTiles( m_TilesFile );
                m_LastPathLoadProject = fileName.left( fileName.lastIndexOf( "/" ) );
        }
}

void CMainWindow::OnGroupTiles()
{
        m_pScreen->GroupTiles();
}

void CMainWindow::OnExportTilesPNG()
{
    QString fileName;


    fileName = QFileDialog::getSaveFileName( this, "Tiles - Export PNG as", m_LastPathLoadProject );

    if( fileName > "" )
    {
        m_LastPathLoadProject = fileName.left( fileName.lastIndexOf( "/" ));
        m_pScreen->ExportTilesPNG( fileName );
    }
}

void CMainWindow::OnGenPaletteFile() {
    QString fileName;


    fileName = QFileDialog::getSaveFileName( this, "Tiles - Create PNG Palette file as", m_LastPathLoadProject );

    if( fileName > "" )
    {
        m_LastPathLoadProject = fileName.left( fileName.lastIndexOf( "/" ));
        m_pScreen->GenPNGPaletteFile( fileName );
    }

}

void CMainWindow::OnExportTilesData()
{
        QString fileName;


        fileName = QFileDialog::getSaveFileName( this, "Tiles - Export to ASM as", m_LastPathExport );

        if( fileName > "" )
        {
            m_LastPathExport = fileName.left( fileName.lastIndexOf( "/" ) );
                        m_pScreen->ExportTilesData( fileName, false );
        }
}

void CMainWindow::OnExportTilesDataHexa()
{
        QString fileName;


                fileName = QFileDialog::getSaveFileName( this, "Tiles - Export to ASM as", m_LastPathExport );

                if( fileName > "" )
                {
                        m_LastPathExport = fileName.left( fileName.lastIndexOf( "/" ) );
                        m_pScreen->ExportTilesData( fileName, true );
                }
}

void CMainWindow::OnExportTilesBin()
{
        QString fileName;


                fileName = QFileDialog::getSaveFileName( this, "Tiles - Export to bin data as", m_LastPathExport );

        if( fileName > "" )
        {
            m_LastPathExport = fileName.left( fileName.lastIndexOf( "/" ) );
            m_pScreen->ExportTilesBin( fileName );
        }

}

void CMainWindow::OnExportTilesBinPletter()
{
                QString fileName;


                fileName = QFileDialog::getSaveFileName( this, "Tiles - Export to bin data compressed with Pletter as", m_LastPathExport );

                if( fileName > "" )
                {
                        m_LastPathExport = fileName.left( fileName.lastIndexOf( "/" ) );
                        m_pScreen->ExportTilesBinPletter( fileName );
                }

}

void CMainWindow::OnImportTilesPNG()
{
    QString fileName;


    fileName = QFileDialog::getOpenFileName( this, "Tiles - Import PNG file created with nMSXtiles", m_LastPathLoadProject, "Image (*.png);;All files (*.*)"  );

    if( fileName == ""  )
    {
        QMessageBox::critical( this, "Error", "No file selected!" );
        return;
    }

    m_LastPathLoadProject = fileName.left( fileName.lastIndexOf( "/" ) );

    if( !m_pScreen->ImportTiles( fileName ) )
    {
        QMessageBox::critical( this, "Error", "Not supported format file" );
    }

    OnReorderColors();
}
/*
void CMainWindow::OnImportTilesPNGAdv()
{
    QString fileName;


    fileName = QFileDialog::getOpenFileName( this, "Tiles - Import PNG file withour palette", m_LastPath, "Image (*.png);;All files (*.*)"  );

    if( fileName == ""  )
    {
        QMessageBox::critical( this, "Error", "No file selected!" );
        return;
    }

    m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );

    if( !m_pScreen->ImportTilesPNGAdv( fileName ) )
    {
        QMessageBox::critical( this, "Error", "Not supported format file" );
    }
}
*/
void CMainWindow::OnImportTiles()
{
        QString fileName;
        QString fileNamePalette;


        QMessageBox::information( this, "", "First you will be prompted for an Image with Palette (see manual). Then you will be prompted for the Tiles Image" );

        fileNamePalette = QFileDialog::getOpenFileName( this, "Tiles - Import Image with Palette", m_LastPathLoadProject, "Palette (*.png);;All files (*.*)" );

        if( fileNamePalette == "" )
        {
                QMessageBox::critical( this, "Error", "No palette, no image!" );
                return;
        }

        m_LastPathLoadProject = fileNamePalette.left( fileNamePalette.lastIndexOf( "/" ) );

        fileName = QFileDialog::getOpenFileName( this, "Tiles - Import Image with Tiles", m_LastPathLoadProject, "Image (*.png);;All files (*.*)"  );

        if( fileName == ""  )
        {
                QMessageBox::critical( this, "Error", "No file selected!" );
                return;
        }

        m_LastPathLoadProject = fileName.left( fileName.lastIndexOf( "/" ) );

        if( !m_pScreen->ImportTiles( fileName, fileNamePalette ) )
        {
                QMessageBox::critical( this, "Error", "Not supported format file" );
        }
}

void CMainWindow::OnUndo()
{
        m_pScreen->Undo();
}

void CMainWindow::OnReorderColors()
{
    m_pScreen->ReorderColors();
}

void CMainWindow::OnCopyTiles()
{
    m_pScreen->CopyTile();
}

void CMainWindow::OnPasteTiles()
{
        m_pScreen->PasteTile();
}

void CMainWindow::OnPasteTilesColors()
{
        m_pScreen->PasteTileColors();
}

void CMainWindow::OnNewSprites()
{
        if( m_pSprites && QMessageBox::question( this, "Confirm", "Save Sprites?", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )    OnSaveAsSprites();

        if( m_pSprites )
        {
            m_pSprites->Close();
            m_pWks->closeActiveSubWindow();
            delete m_pSprites;
        }

        m_pSprites= new CSpritesW();
        m_pSprites->Initialize();
        m_pWks->addSubWindow( m_pSprites, Qt::Dialog );
        m_pSprites->show();
        m_pSprites->NewSprites();

        m_SpritesFile = "";

        EnableMenuItems();
}

void CMainWindow::OnLoadSprites()
{
        QFile hFile;
        QString line;
        QString fileName;


        if( m_pSprites && QMessageBox::question( this, "Confirm", "Save Sprites?", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )    OnSaveAsSprites();

        fileName = QFileDialog::getOpenFileName( this, "Sprite - Load", m_LastPathLoadProject, "nMSXtiles Sprite (*.spt);;All files (*.*)");

        if( fileName == "" ) return;

        m_LastPathLoadProject = fileName.left( fileName.lastIndexOf( "/" ) );

        m_SpritesFile = fileName;

        if( m_pSprites )
        {
            m_pSprites->close();
            delete m_pSprites;
        }

        m_pSprites= new CSpritesW();
        m_pSprites->Initialize();
        m_pWks->addSubWindow( m_pSprites, Qt::Dialog );
        m_pSprites->show();
        m_pSprites->LoadSprites( m_SpritesFile );

        EnableMenuItems();
}

void CMainWindow::OnSaveSprites()
{
        if( m_SpritesFile == "" ) OnSaveAsSprites();
        else m_pSprites->SaveSprites( m_SpritesFile );
}

void CMainWindow::OnSaveAsSprites()
{
        QString fileName;


        fileName = QFileDialog::getSaveFileName( this, "Sprites - Save as", m_LastPathLoadProject, "nMSXtiles Sprite (*.spt);;All files (*.*)" );

        if( fileName > "" )
        {
            m_LastPathLoadProject = fileName.left( fileName.lastIndexOf( "/" ) );
            m_SpritesFile = fileName;
            m_pSprites->SaveSprites( m_SpritesFile );
        }
}

void CMainWindow::OnCloseSprites()
{
        if( m_pSprites && QMessageBox::question( this, "Confirm", "Save Sprites?", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )    OnSaveSprites();

        m_pSprites->Close();

        delete m_pSprites;
        m_pSprites = NULL;

        EnableMenuItems();

}

void CMainWindow::OnExportSprites()
{
    QString fileName;


    fileName = QFileDialog::getSaveFileName( this, "Sprites - Export as", m_LastPathExport );

    if( fileName > "" )
    {
        m_LastPathExport = fileName.left( fileName.lastIndexOf( "/" ) );
        m_pSprites->ExportSprites( fileName );
    }
}

void CMainWindow::EnableMenuItems()
{

        m_pActLoadProject->setEnabled( true );
        m_pActSaveProject->setEnabled( m_pScreen != NULL );
        m_pActSaveAsProject->setEnabled( m_pScreen != NULL );
        m_pActCloseProject->setEnabled( m_pScreen != NULL );
        m_pActImportPolkaProject->setEnabled( true );
        m_pActExportPolkaProject->setEnabled( m_pScreen != NULL );

        m_pActNewScreen->setEnabled( m_pScreen!= NULL );
        m_pActLoadScreen->setEnabled( m_pScreen != NULL );
        m_pActSaveScreen->setEnabled( m_pScreen != NULL );
        m_pActSaveAsScreen->setEnabled( m_pScreen != NULL );
        //m_pActExportScreenData->setEnabled( m_pScreen != NULL );
        //m_pActExportScreenDataHexa->setEnabled( m_pScreen != NULL );
        //m_pActExportScreenBin->setEnabled( m_pScreen != NULL );
        //m_pActExportScreenBinPletter->setEnabled( m_pScreen != NULL );
        m_pActExportScreen->setEnabled( m_pScreen != NULL );
        m_pActFillScreen1to255->setEnabled( m_pScreen != NULL );
        m_pActExportMapPNG->setEnabled( m_pScreen != NULL );

        m_pActImportTiles->setEnabled( m_pScreen != NULL );
        m_pActNewTiles->setEnabled( m_pScreen != NULL );
        m_pActLoadTiles->setEnabled( m_pScreen != NULL );
        m_pActSaveTiles->setEnabled( m_pScreen != NULL );
        m_pActSaveAsTiles->setEnabled( m_pScreen != NULL );
        m_pActLoadTilesLibrary->setEnabled( m_pScreen != NULL );
        m_pActExportTilesData->setEnabled( m_pScreen != NULL );
        m_pActExportTilesBin->setEnabled( m_pScreen != NULL );
        m_pActExportTilesBinPletter->setEnabled( m_pScreen != NULL );
        m_pActReorderColors->setEnabled( m_pScreen != NULL );
        m_pActCopyTiles->setEnabled( m_pScreen != NULL );
        m_pActPasteTiles->setEnabled( m_pScreen != NULL );
        m_pActPasteTilesColors->setEnabled( m_pScreen != NULL );
        m_pActUndo->setEnabled( m_pScreen != NULL );
        m_pActExportTilesDataHexa->setEnabled( m_pScreen );
        m_pActImportTilesPNG->setEnabled( m_pScreen );
        m_pActExportTilesPNG->setEnabled( m_pScreen );
        m_pActGroupTiles->setEnabled( m_pScreen );
        m_pActExportScreenPNG->setEnabled( m_pScreen );
        //m_pActImportTilesPNGAdv->setEnabled( m_pScreen );

        m_pActNewSprites->setEnabled( true );
        m_pActLoadSprites->setEnabled( true );
        m_pActSaveSprites->setEnabled( m_pSprites != NULL );
        m_pActSaveAsSprites->setEnabled( m_pSprites != NULL );
        m_pActCloseSprites->setEnabled( m_pSprites != NULL );
        m_pActExportSprites->setEnabled( m_pSprites != NULL );
}

void CMainWindow::closeEvent(QCloseEvent *)
{
        OnQuitActions();
}
